package com.gy.assistant.ui.meeting.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import im.zego.zim.entity.ZIMCallInfo;
import im.zego.zim.enums.ZIMCallUserState;
import com.gy.assistant.MyApplication;
import com.gy.assistant.R;

public class MeetingListItemHolder extends RecyclerView.ViewHolder {

    private final TextView tvTitle;
    private final TextView tvTime;
    private final int notMeetingColor;
    private final int meetingColor;

    public MeetingListItemHolder(@NonNull ViewGroup viewGroup) {
        super(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_meeting_list, viewGroup, false));
        tvTitle = itemView.findViewById(R.id.tv_title);
        tvTime = itemView.findViewById(R.id.tv_time);
        notMeetingColor = viewGroup.getResources().getColor(R.color.red);
        meetingColor = viewGroup.getResources().getColor(R.color.black);
    }

    public void setData(ZIMCallInfo info) {
        boolean isNotMeeting;
        if (info.caller.equals(MyApplication.sUserId)) {
            //主叫
            isNotMeeting = info.callUserList.stream()
                    .anyMatch(zimCallUserInfo -> !zimCallUserInfo.userID.equals(MyApplication.sUserId) && (zimCallUserInfo.state == ZIMCallUserState.TIMEOUT || zimCallUserInfo.state == ZIMCallUserState.REJECTED));

        } else {
            // 被叫
            isNotMeeting = info.callUserList.stream()
                    .anyMatch(zimCallUserInfo -> zimCallUserInfo.userID.equals(MyApplication.sUserId) && (zimCallUserInfo.state == ZIMCallUserState.TIMEOUT || zimCallUserInfo.state == ZIMCallUserState.REJECTED));
        }

        if (isNotMeeting) {
            tvTitle.setTextColor(notMeetingColor);
            tvTime.setTextColor(notMeetingColor);
        } else {
            tvTitle.setTextColor(meetingColor);
            tvTime.setTextColor(meetingColor);
        }

        tvTitle.setText(String.format("%s的会议", info.caller));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        String date = sdf.format(new Date(info.createTime));
        tvTime.setText(date);
    }

}
